import {claimUsedPartDealCategory} from '../../Enum';

export const onSelectPart = (records, claimWorkItemDetails, claimMaterialDetails) => {
    records.forEach(r => {
        const indexMaterial = claimMaterialDetails.findIndex(v => v.usedPartId === r.id);
        if(indexMaterial === -1)
            claimMaterialDetails.push({
                isCaused: false,
                usedPartId: r.id,
                usedPartCode: r.code,
                usedPartName: r.name,
                newPartId: r.id,
                newPartCode: r.code,
                newPartName: r.name,
                unit: r.unit,
                price: r.price * r.priceRate,
                quantity: 1,
                materialFee: r.price * r.priceRate,
                usedPartDealCategory: claimUsedPartDealCategory.销毁,
                mgmtRate: r.mgmtFeeRate,
                mgmtFee: r.price * r.mgmtFeeRate * r.priceRate,
                priceRate: r.priceRate,
                totalFee: (r.price * r.priceRate) + (r.price * r.mgmtFeeRate * r.priceRate)
            });
        r.workItems.forEach(v => {
            const index = claimWorkItemDetails.findIndex(o => o.workItemId === v.id);
            if(index === -1)
                claimWorkItemDetails.push({
                    usedPartId: r.id,
                    usedPartCode: r.code,
                    workItemId: v.id,
                    workItemCode: v.code,
                    workItemName: v.name,
                    laborHour: v.laborHour,
                    laborPrice: v.laborPrice,
                    laborFee: v.laborHour / 100 * v.laborPrice,
                    remark: v.remark
                });
        });
    });
    return {
        claimWorkItemDetails: [...claimWorkItemDetails],
        claimMaterialDetails: [...claimMaterialDetails]
    };
};
export const onSelectPartAccessory = (records, details) => {
    records.forEach(r => {
        const index = details.findIndex(v => v.usedPartId === r.id);
        if(index === -1)
            details.push({
                isCaused: false,
                usedPartId: r.id,
                usedPartCode: r.code,
                usedPartName: r.name,
                unit: r.unit,
                price: r.price * r.priceRate,
                usedPartDealCategory: claimUsedPartDealCategory.销毁,
                materialFee: r.price * r.priceRate,
                priceRate: r.priceRate,
                accessorySource: r.partAssistPartSource,
                partId: r.partId,
                minPackSize: r.minPackSize,
                maxQty: r.maxQty,
                usedQty: 0,
                quantity: 0
            });
    });
    return {
        details: [...details]
    };
};
export const onChangeSwitchSubDealer = (dealer, data) => ({
    data: Object.assign({}, data, {
        subDealerId: dealer.id,
        subDealerCode: dealer.code,
        subDealerName: dealer.name,
        vehicleSoldId: '',
        vin: '',
        mileage: '',
        engineCode: '',
        saleDate: '',
        shippingDate: '',
        customerName: '',
        cellPhoneNumber: '',
        vehicleStatus: undefined,
        repairType: undefined,
        subClaimId: '',
        subClaimCode: '',
        claimApplyId: '',
        claimApplyCode: '',
        transmissionSn: '',
        productCategoryCode: '',
        productCategoryName: '',
        annoucementNumber: '',
        operationsNature: undefined,
        isKD: undefined
    })
});
export const onChangeSwitchVehicle = (vehicle, data) => ({
    vehicle: Object.assign({}, data, {
        vehicleSoldId: vehicle.id,
        vin: vehicle.vin,
        mileage: undefined,
        engineCode: vehicle.engineCode,
        saleDate: vehicle.saleDate,
        shippingDate: vehicle.shippingDate,
        customerName: vehicle.customerName,
        cellPhoneNumber: vehicle.cellPhoneNumber,
        vehicleStatus: vehicle.status,
        repairType: undefined,
        claimApplyId: '',
        claimApplyCode: '',
        transmissionSn: data.transmissionSn,
        productCategoryCode: data.productCategoryCode,
        productCategoryName: data.productCategoryName,
        annoucementNumber: data.annoucementNumber,
        operationsNature: data.operationsNature,
        isKD: data.isKD,
        currency: data.currency,
        wholesaleDealerId: data.wholesaleDealerId,
        wholesaleDealerCode: data.wholesaleDealerCode,
        wholesaleDealerName: data.wholesaleDealerName,
        country: data.country,
        shipDate: data.shipDate,
        companyName: data.companyName,
        address: data.address,
        email: data.email,
        salesDealerCode: data.salesDealerCode,
        salesDealerName: data.salesDealerName,
        productionDate: data.productionDate
    })
});
